#include "StdAfx.h"
#include "DrawMinAreaRect2.h"


CDrawMinAreaRect2::CDrawMinAreaRect2(void)
{
}


CDrawMinAreaRect2::~CDrawMinAreaRect2(void)
{
}
BEGIN_MESSAGE_MAP(CDrawMinAreaRect2, CDrawOpenCV)
	ON_WM_PAINT()
END_MESSAGE_MAP()


void CDrawMinAreaRect2::DealwithFrame()
{
	// TODO: Add your command handler code here
	if(m_pFrame == NULL)
	{			
		return ;
	}
	if(m_pShow != NULL)
	{
		cvReleaseImage(&m_pShow);
	} 

	m_pShow = cvCreateImage( cvSize( m_pFrame->width, m_pFrame->height), 8, 3 );

	int i, count = rand()%100 + 1;
	CvPoint pt0, pt;
	CvBox2D box;
	CvPoint2D32f box_vtx[4];
	CvPoint2D32f center;
	CvPoint icenter;
	float radius;

	CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
	CvMat pointMat = cvMat( 1, count, CV_32SC2, points );

	for( i = 0; i < count; i++ )
	{
		pt0.x = rand() % (m_pShow->width/2) + m_pShow->width/4;
		pt0.y = rand() % (m_pShow->height/2) + m_pShow->height/4;
		points[i] = pt0;
	}
	box = cvMinAreaRect2( &pointMat, 0 );
	cvMinEnclosingCircle( &pointMat, &center, &radius );

	cvBoxPoints( box, box_vtx );
	cvZero( m_pShow );
	for( i = 0; i < count; i++ )
	{
		pt0 = points[i];

		cvCircle( m_pShow, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED, CV_AA, 0 );
	}

	pt0.x = cvRound(box_vtx[3].x);
	pt0.y = cvRound(box_vtx[3].y);
	for( i = 0; i < 4; i++ )
	{
		pt.x = cvRound(box_vtx[i].x);
		pt.y = cvRound(box_vtx[i].y);
		cvLine(m_pShow, pt0, pt, CV_RGB(0, 255, 0), 1, CV_AA, 0);
		pt0 = pt;
	}
	icenter.x = cvRound(center.x);
	icenter.y = cvRound(center.y);
	cvCircle( m_pShow, icenter, cvRound(radius), CV_RGB(255, 255, 0), 1, CV_AA, 0 );

}